<?php
namespace app\behavior;
use Hedeqiang\TenIM\IM;
use think\Env;
use think\Log;
use think\Request;
/**
 *  若是该行为类中只有一个行为，只需要定义一个行为入口方法`run`即可
 */
class User
{
    // 当该行为类被绑定到钩子的时候，run行为方法则会在该钩子被触发的时候执行
    public function run($params)
    {
        // 行为逻辑
    }

    //用户注册成功
    public function userRegisterSuccessed($params='')
    {
        $config = [
            'sdk_app_id' => Env::get('im.sdk_app_id', ''),
            'identifier' => Env::get('im.identifier', ''),
            'secret_key' => Env::get('im.secret_key', ''),
        ];
        //注册腾讯IM
        $data = [
            'Identifier' => (string)$params['id'],
            'Nick'=>$params['nickname'],
            'FaceUrl'=>\request()->root(true).'/public/assets/img/avatar.png',
        ];
//        var_dump($data);
//        die;
        $im = new IM($config);
        $res = $im->send('im_open_login_svc','account_import',$data);
        Log::info('导入IM账户：');
        Log::info($data);
        Log::info($res);
    }

    //用户修改资料
    public function userUpdateProfile($params='')
    {
        $config = [
            'sdk_app_id' => Env::get('im.sdk_app_id', ''),
            'identifier' => Env::get('im.identifier', ''),
            'secret_key' => Env::get('im.secret_key', ''),
        ];
//        var_dump($params['avatar']);
//        if (stripos($params['avatar'], 'data:image')){
//            $avatar = '';
//        }else{
//            $avatar = compatImage($params['avatar']);
//        }
//        die;
        //注册腾讯IM
        $data = [
            'From_Account' => (string)$params['id'],
            'ProfileItem' => [
                ['Tag' => 'Tag_Profile_IM_Nick', 'Value' => $params['nickname']],
                ['Tag' => 'Tag_Profile_IM_Image', 'Value' => compatImage($params['avatar'])],
            ],
        ];
//        var_dump($data);
//        die;
        $im = new IM($config);
        $res = $im->send('profile','portrait_set',$data);
        Log::info('IM用户修改资料：'.$params['id']);
        Log::info($res);
    }

}